package com.meihua.shellapp.activity.set;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.set.SetNewPayPwdActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * 作者：lc on 2017/10/19.
 * 邮箱：lc0607live@163.com
 */

public class VerificatoinCodeActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.et_card)
    EditText et_card;//验证码
    @Bind(R.id.tv_get_code)
    TextView tv_get_code;//获取验证码
    @Bind(R.id.tv_message)
    TextView tv_message;//提示信息
    @Bind(R.id.bt_next)
    Button bt_next;//下一步

    private int time;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what > 0) {
                tv_get_code.setBackgroundResource(R.drawable.btn_backgroud_gary_6);
                tv_get_code.setText(msg.what + "");
            } else {
                tv_get_code.setBackgroundResource(R.drawable.btn_backgroud_round_6);
                tv_get_code.setText("获取验证码");
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_verificatoin_phone;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tv_message.setText("请为您的帐号" + MyApplication.getInstance().bean.getPhone().substring(0, 3) + "****" + MyApplication.getInstance().bean.getPhone().substring(7, 11)
                + "设置一个新的支付密码");
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("重置支付密码");
        bt_next.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_get_code://获取验证码
                if (!tv_get_code.getText().toString().equals("获取验证码")) {
                    return;
                }
                sendCode(MyApplication.getInstance().bean.getPhone());
                break;
            case R.id.bt_next://下一步
                if (et_card.getText().toString().equals("")) {
                    ToastUtils.getInstance().showToast("请输入验证码！");
                    return;
                }
                if (et_card.getText().toString().length() < 6) {
                    ToastUtils.getInstance().showToast("验证码为6位数字");
                    return;
                }
                vierificationCode();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendCode(String phone) {
        OkGo.<String>post(URL.getUrl().SENDCODE)
                .params("phone", phone)
                .params("type", 5)
                .execute(new MyStringCallBack<BaseBean>(tv_get_code) {
                    @Override
                    public void onSuccessData(BaseBean baseBean) {
                        super.onSuccessData(baseBean);
                        if (baseBean.getCode() == 1) {
                            countDown(60);//倒计时
                        }
                        ToastUtils.getInstance().showToast(baseBean.getMsg());
                    }
                });
    }

    /**
     * 倒计时
     *
     * @param timetemp 时间(s)
     */
    private void countDown(int timetemp) {
        time = timetemp;
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (time < 0) {
                    return;
                }
                time--;
                try {
                    Thread.sleep(1000);
                    handler.sendEmptyMessage(time);
                    run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }

            }
        }).start();
    }


    public void vierificationCode() {
        OkGo.<String>post(URL.getUrl().CHECKPHONE)
                .params("phone", MyApplication.getInstance().bean.getPhone())
                .params("type", 5)
                .params("mid", MyApplication.getInstance().getMId())
                .params("code", et_card.getText().toString())
                .execute(new MyStringCallBack<BaseBean>(bt_next) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            Intent in = new Intent(mContext, SetNewPayPwdActivity.class);
                            startActivity(in);
                            finish();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
